<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="冷库名称" prop="coldName">
        <el-input
          v-model="queryParams.coldName"
          placeholder="请输入冷库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="冷库描述" prop="coldDescript">
        <el-input
          v-model="queryParams.coldDescript"
          placeholder="请输入冷库描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="冷库地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入冷库地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小面积" prop="area1">
        <el-input
          v-model="queryParams.area1"
          placeholder="请输入最小占地面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大面积" prop="area2">
        <el-input
          v-model="queryParams.area2"
          placeholder="请输入最大占地面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最小容积" prop="totalVolume1">
        <el-input
          v-model="queryParams.totalVolume1"
          placeholder="请输入最小容积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大容积" prop="totalVolume2">
        <el-input
          v-model="queryParams.totalVolume2"
          placeholder="请输入最大容积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="冷库状态" clearable>
          <el-option
            v-for="dict in dict.type.lg_storage_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" width="80">
        <template slot-scope="scope">
          {{ (scope.$index + 1) + (queryParams.pageNum - 1) * queryParams.pageSize }}
        </template>
      </el-table-column>
      <el-table-column label="冷库id" align="center" prop="coldId" />
      <el-table-column label="冷库名称" align="center" prop="coldName">
        <template slot-scope="scope">
          <el-button type="text" @click="getMap(scope.row)">{{ scope.row.coldName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="冷库地址" align="center" prop="address" />
      <el-table-column label="占地面积" align="center" prop="area">
        <template slot-scope="scope">
          {{ scope.row.area }} 平方米
        </template>
      </el-table-column>
      <el-table-column label="总容积" align="center" prop="totalVolume">
        <template slot-scope="scope">
          {{ scope.row.totalVolume }} 立方米
        </template>
      </el-table-column>
      <!--      <el-table-column label="负责人id" align="center" prop="managerId" />-->
      <el-table-column label="负责人姓名" align="center" prop="managerName" />
      <!--      <el-table-column label="负责人" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--          {{ this.storageUserList.find(item => item.id === this.scope.row.managerId).userName}}-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_storage_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="submitForm(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getTotalSome"
    />

    <!-- 点击冷库名字弹出地图 -->
    <el-dialog :title="title" :visible.sync="open2" width="1000px"  append-to-body>
      <baidu-map
        class="map2"
        :center="{ lng: mapX, lat: mapY }"
        :zoom="15"
        :scroll-wheel-zoom="true"
        mapType="BMAP_NORMAL_MAP"
        @click="handleMapClick"
      >
        <!-- 添加地图控件 -->
        <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
        <bm-scale anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-scale>
        <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>

        <!-- 添加定位控件 -->
        <bm-geolocation
          anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
          :showAddressBar="true"
          :autoLocation="true"
        ></bm-geolocation>

        <!-- 添加路况图层 -->
        <bm-traffic :type="0"></bm-traffic>

        <!-- 添加卫星图层（可选） -->
        <bm-overview-map anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-overview-map>
      </baidu-map>
      <div slot="footer" class="dialog-footer" style="padding-left: 330px;padding-right: 0">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getStorageUserList, getTotal, listPost} from "@/api/storage/bigCold";
import {
  getBoundCold,
  updateBigCold
} from "@/api/equipment/coldMachine";
import store from "@/store";
export default {
  name: "Post",
  props: {
    mid: {
      type: [Number, String], // 根据实际情况调整类型
      default: null
    }
  },
  dicts: ['sys_normal_disable', 'lg_storage_status'],
  data() {
    return {
      clickedLocation: null, // 用于存储点击位置的经纬度
      userId: store.state.user.id, // 将缓存的用户id赋值给userId
      userName: store.state.user.name, // 将缓存的用户id赋值给userId
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 大冷库表格数据
      storageList: [],
      // 冷库管理员名单
      storageUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open2: false,
      // 弹出的地图的经纬度
      mapX: undefined,
      mapY: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coldName: undefined,
        coldDescript: undefined,
        address: undefined,
        area1: undefined,
        area2: undefined,
        totalVolume1: undefined,
        totalVolume2: undefined,
        managerId: undefined,
        status: undefined
      },
    };
  },
  created() {
    this.getList();
    this.getTotalSome();
    this.getStorageUserListSome();
  },
  methods: {
    //点击冷库名字弹出地图
    getMap(some) {
      this.open2 = true;
      this.mapX = some.coordinateX;
      this.mapY = some.coordinateY;
      console.log(this.mapX);
    },
    //获取点击位置经纬度
    handleMapClick(e) {
      // 获取点击位置的经纬度
      const {lng, lat} = e.point;
      this.clickedLocation = {lng, lat};
      // console.log("点击位置的经度：", lng);
      // console.log("点击位置的纬度：", lat);
    },
    /** 查询所有冷库 */
    getStorage() {
      getBoundCold(this.queryParams).then(response => {
        this.storageList = response.data;
        // console.log(response);
        //将负责人姓名塞进去
        for (var i = 0; i < this.storageList.length; i++) {
          for (var j = 0; j < this.storageUserList.length; j++) {
            if (this.storageList[i].managerId == this.storageUserList[j].userId) {
              this.storageList[i].managerName = this.storageUserList[j].userName;
              // console.log(this.storageList[i].managerName);
            }
          }
        }
      });
    },
    /** 查询冷库管理员名单 */
    getStorageUserListSome() {
      getStorageUserList().then(response => {
        this.storageUserList = response.data;
      });
      this.getStorage();
    },
    //获取数据总条数
    getTotalSome() {
      // console.log(this.queryParams);
      this.loading = true;
      getTotal(this.queryParams).then(response => {
        this.getStorage();
        this.total = response.data;
        // alert(this.total);
        // console.log(response);
        this.loading = false;
      });
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open2 = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getTotalSome();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.coldId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    submitForm(row) {
      console.log("coldId",row.coldId);
      updateBigCold({
        mid: this.mid,
        coldId: row.coldId,
        updateBy: this.$store.state.user.name,
      }).then(() => {
        console.log(row.coldId);
        this.$message.success('绑定成功');
        this.$emit('selected', row.coldId);
        // this.$emit('close'); // 关键代码：触发自定义事件
      }).catch(() => {
        this.$message.error('绑定失败');
      });
    }
  }
};
</script>

<style scoped>
.map2 {
  width: 100%;
  height: 500px;
}
</style>
